import { createApp } from 'vue';
import { getApp } from '@tarojs/taro';
import { redirectTo, history } from '@tarojs/router';
import setTitle from 'gdt-jsapi/setTitle';
import VConsole from 'vconsole';
import '@nutui/touch-emulator';
import '/@/assets/scss/index.scss';
import {
  AppNavbar,
  AppTabBar,
  AppUploadImage,
  AppRegionItem,
  AppRegionListPopup,
  AppRegionListSelectorPopup,
  AppAssetListPopup,
  AppArchivesListPopup,
  AppAssetItem,
  AppArchivesItem,
  AppWorkOrderEventList,
  AppApproveList,
  AppAssetChangeOperateList,
  AppSpareItem,
  AppSpareListPopup,
  AppSpareStockListPopup,
  AppSpareStockItem,
  AppUserListPopup,
  AppOrganCascader,
  AppAliPlayer,
  AppLicensePlate,
  AppDdRecordAudio,
  AppWxRecordAudio,
  AppFaceUploadList,
  AppArchiveListPopup,
  AppDeliverEventList,
  AppTimeRangePickerPopup,
  AppEChart,
  AppTimeRangeFilter,
  AppUploadSignature,
  AppRegionTreePopup,
} from '/@/components/index';
import { loadLink, loadScript } from '/@/utils/asset-load';
import { isMobile } from '/@/utils/index';
import '/@/utils/hgy-dd-aplus-cloud';

if (process.env.TARO_ENV === 'h5') {
  if (window.g_config && window.g_config.cdnHost) {
    loadScript(
      `//${window.g_config.cdnHost}/gdt/dd-web-dev-center/0.3.1/assets/js/app_2405f57c-18bd-497d-9bf3-28fcf83fc407.js`,
      {
        async: false,
        defer: true,
      },
    );
  }

  loadLink(`${process.env.APP_API_URL}common/js/aliplayer/skins/default/aliplayer-min.css`);

  loadScript(`${process.env.APP_API_URL}common/js/aliplayer/aliplayer-min.js`, {
    async: false,
    defer: true,
  });

  window.onresize = () => {
    document.body.style.maxWidth = isMobile() ? '' : '550px';
  };
  window.onload = () => {
    document.body.style.maxWidth = isMobile() ? '' : '550px';
  };
}

let vConsole: VConsole;

const App = createApp({
  mounted() {
    if (process.env.TARO_ENV === 'h5') {
      history.listen(({ location }) => {
        const currentRouter = getApp().config.routes.filter(
          (r: { path: string; navigationBarTitleText?: string }) => {
            return `/${r.path}` === location.pathname;
          },
        )[0];

        if (process.env.APP_EDITION === 'hgy') {
          setTitle({
            title: currentRouter.navigationBarTitleText || process.env.APP_TITLE,
          });
        } else {
          document.title = currentRouter.navigationBarTitleText || process.env.APP_TITLE;
        }
      });
    }
  },
  onShow() {
    if (
      process.env.TARO_ENV === 'h5' &&
      process.env.APP_EDITION === 'hgy' &&
      ['development', 'test', 'production'].includes(process.env.APP_ENV as string)
    ) {
      vConsole = new VConsole();
    }

    if (process.env.TARO_ENV === 'h5') {
      document.title = process.env.APP_TITLE || '加载中...';
    }
  },
  onHide() {
    if (
      process.env.TARO_ENV === 'h5' &&
      process.env.APP_EDITION === 'hgy' &&
      ['development', 'test', 'production'].includes(process.env.APP_ENV as string)
    ) {
      vConsole.destroy();
    }
  },
  // 入口组件不需要实现 render 方法，即使实现了也会被 taro 所覆盖
  onPageNotFound() {
    redirectTo({
      url: '/pages/common/404/index',
    });
  },
});

App.component('app-navbar', AppNavbar)
  .component('app-tab-bar', AppTabBar)
  .component('app-upload-image', AppUploadImage)
  .component('app-region-list-popup', AppRegionListPopup)
  .component('app-region-list-selector-popup', AppRegionListSelectorPopup)
  .component('app-region-item', AppRegionItem)
  .component('app-asset-list-popup', AppAssetListPopup)
  .component('app-archives-list-popup', AppArchivesListPopup)
  .component('app-asset-item', AppAssetItem)
  .component('app-archives-item', AppArchivesItem)
  .component('app-spare-list-popup', AppSpareListPopup)
  .component('app-spare-item', AppSpareItem)
  .component('app-spare-stock-list-popup', AppSpareStockListPopup)
  .component('app-spare-stock-item', AppSpareStockItem)
  .component('app-work-order-event-list', AppWorkOrderEventList)
  .component('app-asset-change-operate-list', AppAssetChangeOperateList)
  .component('app-user-list-popup', AppUserListPopup)
  .component('app-ali-player', AppAliPlayer)
  .component('app-license-plate', AppLicensePlate)
  .component('app-organ-cascader', AppOrganCascader)
  .component('app-approve-list', AppApproveList)
  .component('app-face-upload-list', AppFaceUploadList)
  .component('app-deliver-event-list', AppDeliverEventList)
  .component('app-dd-record-audio', AppDdRecordAudio)
  .component('app-wx-record-audio', AppWxRecordAudio)
  .component('app-archive-list-popup', AppArchiveListPopup)
  .component('app-time-range-picker-popup', AppTimeRangePickerPopup)
  .component('app-e-chart', AppEChart)
  .component('app-time-range-filter', AppTimeRangeFilter)
  .component('app-upload-signature', AppUploadSignature)
  .component('app-region-tree-popup', AppRegionTreePopup);

App;

export default App;
